﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class UserList : BasePage
    {
        private string companyid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            companyid = Request.QueryString["id"] + "";
            if (!Page.IsPostBack)
            {
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            //设置控件属性，从BasePage中得到
            PageConfig(UPager);

            //List<Member> list = new List<Member();
            UserCompanyHelper uchelper = new UserCompanyHelper();
            Criteria c = new Criteria();

            c.Add(CriteriaType.Equals, "Status", 1);
            if (companyid != "")
            {
                c.Add(CriteriaType.Equals, "CompanyID", companyid);
            }

            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();
            string nickname = txtNickName.Text.Trim();

            Users uinfo = UserBase.GetCurrentUser();
            c.Add(CriteriaType.Equals, "CompanyOwnerID", uinfo.ID);

            if (!string.IsNullOrEmpty(begindate))
            {
                try
                {
                    DateTime from = Convert.ToDateTime(begindate);
                    // 直接输入日期类型
                    c.Add(CriteriaType.MoreThanEquals, "CreateDate", from);
                }
                catch (Exception ex)
                {
                    Tools.Alert("日期格式不正确");
                    throw ex;
                }

            }
            if (!string.IsNullOrEmpty(enddate))
            {
                try
                {
                    DateTime end = Convert.ToDateTime(enddate);
                    // 直接输入日期类型
                    c.Add(CriteriaType.LessThanEquals, "CreateDate", end);
                }
                catch (Exception ex)
                {
                    Tools.Alert("日期格式不正确");
                    throw ex;
                }
            }
            if (!string.IsNullOrEmpty(nickname))
            {
                c.Add(CriteriaType.Like, "NickName", "%" + nickname + "%");
            }

            UPager.ItemCount = uchelper.QueryUserCompanyCountBySearch(c);

            // 以下的值从BasePage中得到
            UPager.UrlFormat = UrlFormat;
            UPager.PrefixText = GetPreFixText(UPager);

            if (mark)
            {
                PagerInit(UPager);
            }

            string[] fields = new string[] { "ID", "UsersID", "NickName", "CompanyID", "CreateDate" };

            DataTable dt = new DataTable();
            dt = uchelper.QueryUserCompanyBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, UPager.Begin - 1, UPager.Count, fields);

            dt.Columns.Add(new DataColumn("CompanyName"));
            dt.Columns.Add(new DataColumn("blognum"));
            dt.Columns.Add(new DataColumn("blogpvnum"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string getcompanyid = dr["CompanyID"] + "";
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(getcompanyid);
                    string companyname = cinfo.CompanyName;
                    dr["CompanyName"] = companyname;

                    //加载该用户在该小组内创作的数量
                    string userid = dr["UsersID"] + "";
                    BlogHelper bhelper = new BlogHelper();
                    Criteria cblog = new Criteria();
                    cblog.Add(CriteriaType.Equals, "UsersID", userid);
                    cblog.Add(CriteriaType.Equals, "CompanyID", companyid);
                    cblog.Add(CriteriaType.Equals, "Flag", 0);

                    string[] blogfields = new string[] { "ID", "UsersID", "CompanyID", "PVCount", "CreateDate" };
                    DataTable dtblog = bhelper.QueryBlogBySearchToTable(cblog, null, 0, 0, null);
                    int blognum = 0;
                    int blogpvnum = 0;

                    if (dtblog != null && dtblog.Rows.Count > 0)
                    {
                        blognum = dtblog.Rows.Count;
                        foreach (DataRow drblog in dtblog.Rows)
                        {
                            int pvcount = Convert.ToInt32(drblog["PVCount"] + "");
                            blogpvnum += pvcount;
                        }
                    }

                    dr["blognum"] = blognum.ToString();
                    dr["blogpvnum"] = blogpvnum.ToString();
                }
            }

            rptUserList.DataSource = dt;
            rptUserList.DataBind();
        }
        #endregion

        #region 查询
        protected void ibSearch_Click(object sender, ImageClickEventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }
        #endregion

        #region 查询条件设置
        private void SetCondition()
        {
            string nickname = txtNickName.Text.Trim();
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] =nickname+"@"+ begindate + "@" + enddate + "@" + "UserList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 4)
            {
                string page = conditionAll[3];
                if (page == "UserList")
                {
                    txtNickName.Text = conditionAll[0];
                    txtBeginDate.Text = conditionAll[1];
                    txtEndDate.Text = conditionAll[2];
                }
            }
        }
        #endregion

        #region 列表事件
        protected void rptUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HiddenField hidUsersID = (HiddenField)e.Item.FindControl("hidUsersID");
            string id = hidID.Value;
            string usersid = hidUsersID.Value;

            if (e.CommandName == "cancel")//删除成员
            {
                Users uinfo = UserBase.GetCurrentUser();

                UserCompany ucinfo = new UserCompany();
                UserCompanyHelper uchelper = new UserCompanyHelper();
                ucinfo = uchelper.GetUserCompany(id);

                string companyid = ucinfo.CompanyID;
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(companyid);
                string companyname = cinfo.CompanyName;

                //给成员发送消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo.FromUsersID = uinfo.ID;
                minfo.FromNickName = uinfo.NickName;
                minfo.ToUsersID = ucinfo.UsersID;
                minfo.MessageContent = "您被退出了小组：<a href=\"../c/" + companyid + ".aspx\" target=\"_blank\">" + companyname + "</a>";
                minfo.Status = 0;
                mhelper.AddMessage(minfo);

                uchelper.DeleteUserCompany(ucinfo);//删除 

                //该公司的用户数减1
                int usernum = cinfo.UserNum;
                usernum = usernum - 1;
                cinfo.UserNum = usernum;
                chelper.EditCompanys(cinfo);

                Tools.AlertAndRedirect("删除成员成功", "UserList.aspx?id="+companyid);
            }
            else if (e.CommandName == "sendmoney")//奖励
            {
                Response.Redirect("SendMoney.aspx?touserid=" + usersid + "&companyid="+companyid);
                return;
            }
            else if (e.CommandName == "sendmessage")//发送短消息
            {
                Response.Redirect("../UserSetting/MessageReply.aspx?touserid=" + usersid + "&source=companylist");
                return;
            }
        }
        #endregion
    }
}